package tsg.component;

import java.util.ArrayList;

import tsg.event.WindowEvent;
import tsg.event.WindowListener;
import tsg.graphic.Image;
import tsg.layout.FillParentLayout;
import tsg.util.ObjectUtilities;

public abstract class Window extends Container
{
	public Window()
	{
		this("", null);
	}
	
	public Window(String text)
	{
		this(text, null);
	}
	
	public Window(String text, Window parentWindow)
	{
		super(new FillParentLayout());
		this.setText(text);
		_parentWindow = parentWindow;
	}
	
	public Window(Window parentWindow)
	{
		this("", null);
	}
	
	public Window getParentWindow()
	{
		return _parentWindow;
	}
	
	public void addWindowListener(WindowListener windowListener)
	{
		ObjectUtilities.denullify(_windowListeners, WindowListener.class).add(windowListener);
	}
	
	public void removeWindowListener(WindowListener windowListener)
	{
		ObjectUtilities.denullify(_windowListeners, WindowListener.class).remove(windowListener);
	}
	
	public void fireWindowEvent(WindowEvent e)
	{
		// TODO
	}
	
	protected void addChildWindow(Window window)
	{
		ObjectUtilities.denullify(_childWindows, Window.class).add(window);
	}
	
	protected void removeChildWindow(Window window)
	{
		ObjectUtilities.denullify(_childWindows, Window.class).remove(window);
	}
	
	public Window[] getChildWindows()
	{
		_childWindows = ObjectUtilities.denullify(_childWindows, ArrayList.class);
		
		return _childWindows.toArray(new Window[_childWindows.size()]);
	}
	
	public void setAlwaysOnTop(boolean alwaysOnTop)
	{
		_alwaysOnTop = alwaysOnTop;
	}
	
	public boolean getAlwaysOnTop()
	{
		return _alwaysOnTop;
	}
	
	protected Image[] getImages()
	{
		_images = ObjectUtilities.denullify(_images, ArrayList.class);
		
		return _images.toArray(new Image[_images.size()]);
	}
	
	protected void setImage(Image image)
	{
		_images = ObjectUtilities.denullify(_images, ArrayList.class);
		
		_images.clear	();
		_images.add		(image);
	}
	
	protected void addImage(Image image)
	{
		ObjectUtilities.denullify(_images, ArrayList.class).add(image);
	}
	
	public void setText(String text)
	{
		_text = text;
	}
	
	public String getText()
	{
		return _text;
	}
	
	// TODO: toBack and toFront
	
	private ArrayList<WindowListener>	_windowListeners	= null;
	private ArrayList<Window>			_childWindows		= null;
	private ArrayList<Image>			_images				= null;
	private String						_text				= null;
	private Window						_parentWindow		= null;
	private boolean						_alwaysOnTop		= false;
}








